// pages/seal/company/company.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedReasons: [], // 存储选中的刻章原因
    region: ['请选择省份', '请选择城市', '请选择区县'],
    regionDisplay: '请选择地区', // 添加用于显示的格式化值
    customItem: '全部',
    reason: [
      { value: 1, name: '新刻（从未刻制过印章）', checked: false },
      { value: 2, name: '变更（执照在工商变更过）', checked: false },
      { value: 3, name: '损坏（旧章无法使用或章面损坏）', checked: false },
      { value: 4, name: '遗失（原备案章遗失）', checked: false },
      { value: 5, name: '增刻（根据业务需要多枚印章）', checked: false }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 接收sealType参数
    if (options.sealType) {
      this.setData({
        sealType: options.sealType
      });
      // 读取缓存并回显
      const cache = wx.getStorageSync('sealFormCache') || {};
      if (cache[options.sealType]) {
        this.setData({
          ...cache[options.sealType]
        });
      }
      console.log('接收到的印章类型ID:', options.sealType);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 缓存当前表单数据到sealType下
  saveToCache() {
    const { sealType, selectedReasons, region, regionDisplay, reason, licenseName, bossPhone } = this.data;
    let cache = wx.getStorageSync('sealFormCache') || {};
    cache[sealType] = {
      selectedReasons,
      region,
      regionDisplay,
      reason,
      licenseName,
      bossPhone
    };
    wx.setStorageSync('sealFormCache', cache);
  },

  onInputChange: function(e) {
    const { field } = e.currentTarget.dataset;
    const { value } = e.detail;
    this.setData({
      [field]: value
    }, () => {
      this.saveToCache();
    });
  },

  checkboxChange: function(e) {
    const values = e.detail.value;
    const reason = this.data.reason.map(item => ({
      ...item,
      checked: values.includes(item.value.toString())
    }));
    this.setData({ 
      reason,
      selectedReasons: values
    }, () => {
      this.saveToCache();
    });
  },

  // 城市选择
  bindRegionChange: function(e) {
    const selectedRegion = e.detail.value;
    this.setData({
      region: selectedRegion,
      regionDisplay: selectedRegion.join('-')
    }, () => {
      this.saveToCache();
    });
  },

  nextStep: function() {
    // 获取表单数据
    const formData = {
      region: this.data.region,
      licenseName: this.data.licenseName,
      bossPhone: this.data.bossPhone,
      reasons: this.data.reason.filter(item => item.checked).map(item => item.value)
    };

    // 验证必填项
    if (!formData.region[0] || !formData.region[1] || !formData.region[2]) {
      wx.showToast({
        title: '请选择执照地区',
        icon: 'none'
      });
      return;
    }

    if (!formData.licenseName) {
      wx.showToast({
        title: '请输入执照名称',
        icon: 'none'
      });
      return;
    }

    if (!formData.bossPhone) {
      wx.showToast({
        title: '请输入法人电话',
        icon: 'none'
      });
      return;
    }

    if (formData.reasons.length === 0) {
      wx.showToast({
        title: '请选择刻章原因',
        icon: 'none'
      });
      return;
    }

    // 保存到全局数据
    const app = getApp();
    app.globalData.companyInfo = formData;

    // 跳转到 classify 页面
    wx.navigateTo({
      url: '/pages/seal/classify/classify?sealType=' + this.data.sealType
    });
  }

})